package com.boronstream.istar.http;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.util.Log;

import com.boronstream.istar.http.PlayClient.BaseHttpClient;

public class WebUtils {

	private static final String TAG = WebUtils.class.getSimpleName();

	/**
	 * 
	 * @param url
	 * @param params
	 * @return
	 * @throws IOException
	 */
	public static HashMap<String, String> doGet(String url,
			Map<String, Object> params) throws IOException,
			NullPointerException,Exception {

		url += genrateUrlparams(params);
		Log.d(TAG, "doGet,Url:" + url);
		HttpGet httpGet = new HttpGet(url);
		BaseHttpClient client = new BaseHttpClient();
		HttpResponse response = client.execute(httpGet);

		int status_code = response.getStatusLine().getStatusCode();
		HttpEntity entity = response.getEntity();
		String body = IOUtils.toString(entity.getContent(), HTTP.UTF_8);

		HashMap<String, String> resHashMap = new HashMap<String, String>();
		resHashMap.put("body", body);
		resHashMap.put("status_code", String.valueOf(status_code));
		return resHashMap;
	}

	/**
	 * 
	 * @param url
	 * @param headers
	 * @param params
	 * @return
	 * @throws IOException
	 */
	public static HashMap<String, String> doGet(String url,
			Map<String, String> headers, Map<String, Object> params)
			throws IOException,Exception {
		url += genrateUrlparams(params);
		Log.d(TAG, "doGet,Url:" + url + "\nheader:" + headers);
		HttpGet httpGet = new HttpGet(url);
		Set<String> headerKeys = headers.keySet();
		for (String key : headerKeys) {
			httpGet.setHeader(key, headers.get(key));
		}

		BaseHttpClient client = new BaseHttpClient();
		HttpResponse response = client.execute(httpGet);

		int status_code = response.getStatusLine().getStatusCode();
		HttpEntity entity = response.getEntity();
		String body = IOUtils.toString(entity.getContent(), HTTP.UTF_8);

		HashMap<String, String> resHashMap = new HashMap<String, String>();
		resHashMap.put("body", body);
		resHashMap.put("status_code", String.valueOf(status_code));
		Log.d(TAG, "doGet, before exit, resHashMap == " + resHashMap);
		return resHashMap;
	}

	/**
	 * 
	 * @param url
	 * @param params
	 * @return
	 * @throws IOException
	 */
	public static String doPost(String url, Map<String, String> params)
			throws IOException {
		return null;
	}

	/**
	 * 
	 * @param url
	 * @param headers
	 * @param params
	 * @return
	 * @throws IOException
	 */
	public static HashMap<String, String> doPost(String url,
			Map<String, String> headers, Map<String, Object> params)
			throws IOException {
		Log.d(TAG, "doPost,Url:" + url + "\nheader:" + headers + "\nparams:"
				+ params);

		/**
		 * Post
		 */
		HttpPost httpPost = new HttpPost(url);
		Set<String> headerKeys = headers.keySet();
		for (String key : headerKeys) {
			httpPost.setHeader(key, headers.get(key));
		}

		/**
		 * Params
		 */
		// HttpParams httpParams = new BasicHttpParams();
		// Set<String> paramsKeys = params.keySet();
		// for (String key : paramsKeys) {
		// httpParams.setParameter(key, params.get(key));
		// }

		// new Nam

		List<NameValuePair> httpParams = new ArrayList<NameValuePair>();
		Set<String> paramsKeys = params.keySet();
		for (String key : paramsKeys) {
			httpParams.add(new BasicNameValuePair(key, String.valueOf(params
					.get(key))));
		}
		httpPost.setEntity(new UrlEncodedFormEntity(httpParams, HTTP.UTF_8));

//		BaseHttpClient client = new BaseHttpClient();
		HttpClient client = new DefaultHttpClient();
		HttpResponse response = client.execute(httpPost);

		int status_code = response.getStatusLine().getStatusCode();
		HttpEntity entity = response.getEntity();
		String body = IOUtils.toString(entity.getContent(), HTTP.UTF_8);

		HashMap<String, String> resHashMap = new HashMap<String, String>();
		resHashMap.put("body", body);
		resHashMap.put("status_code", String.valueOf(status_code));
		return resHashMap;
	}

	public static HashMap<String, String> doPostJson(String url,
			Map<String, String> headers, String jsonString) throws Exception {
		Log.d(TAG, "doPost,Url:" + url + "\nheader:" + headers);

		/**
		 * Post
		 */
		HttpPost httpPost = new HttpPost(url);
		Set<String> headerKeys = headers.keySet();
		for (String key : headerKeys) {
			httpPost.setHeader(key, headers.get(key));
		}

		/**
		 * Params
		 */
		httpPost.setEntity(new StringEntity(jsonString, HTTP.UTF_8));
		Log.i(TAG, "postJson:" + jsonString);

		BaseHttpClient client = new BaseHttpClient();
		HttpResponse response = client.execute(httpPost);

		int status_code = response.getStatusLine().getStatusCode();
		HttpEntity entity = response.getEntity();
		String body = IOUtils.toString(entity.getContent(), HTTP.UTF_8);

		HashMap<String, String> resHashMap = new HashMap<String, String>();
		resHashMap.put("body", body);
		resHashMap.put("status_code", String.valueOf(status_code));
		return resHashMap;
	}

	/**
	 * 
	 * @param params
	 * @return
	 */
	private static String genrateUrlparams(Map<String, Object> params) {
		if (params == null || params.size() < 1) {
			return "";
		}
		StringBuffer urlString = new StringBuffer("?");
		Set<String> keySet = params.keySet();
		for (String key : keySet) {
			try {
				urlString.append(key
						+ "="
						+ URLEncoder.encode(String.valueOf(params.get(key)),
								HTTP.UTF_8) + "&");
			} catch (Exception e) {
			}
		}
		urlString = new StringBuffer(urlString.reverse().toString()
				.replaceFirst("&", ""));
		return urlString.reverse().toString();
	}

}
